package com.hg.concurent.demo6;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * created by skh on 2019/12/22
 */
@RestController
public class JucController {

	private int num = 100;

	private AtomicInteger atomicInteger = new AtomicInteger(100);

	private List<Integer> list = new ArrayList();

	private List jucList = new CopyOnWriteArrayList();



	public static void main(String[] args) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("子线程运行中");
				ThreadUtil.sleep(5000);
				System.out.println("子线程5秒后苏醒");

			}
		}).start();

		//主线程
		ThreadUtil.sleep(100);
		System.out.println("主线程结束");


	}

	//测试实例变量
	@RequestMapping("/test1")
	public Integer test1() throws InterruptedException {
		if (num > 0) {
			this.wait();
			num--;
		}
		System.out.println("num = " + num);
		return num;
	}

	@RequestMapping("/getNum")
	public Integer getNum() {
		System.out.println("num = " + num);
		return num;
	}

	//测试atomic实例变量
	@RequestMapping("/test2")
	public Integer test2() {
		if (atomicInteger.get() > 0) {
			int i = atomicInteger.decrementAndGet();
			System.out.println("i = " + i);
		}
		return 1;
	}

	@RequestMapping("/getNum2")
	public Integer getNum2() {
		int i = atomicInteger.get();
		System.out.println("i = " + i);
		return i;
	}

	//测试集合
	@RequestMapping("/test3")
	public Integer test3() {
		list.add(RandomUtil.randomInt());
//		jucList.add(RandomUtil.randomInt());
		return 0;
	}

	@RequestMapping("/getList")
	public Integer test4() {
		System.out.println(list);
		System.out.println(list.size());
		System.out.println("456");
		return 0;
	}
}
